package com.ncepu.clubmanagement.controller;

import com.ncepu.clubmanagement.exception.NotExistException;
import com.ncepu.clubmanagement.pojo.Affair;
import com.ncepu.clubmanagement.pojo.AffairContent;
import com.ncepu.clubmanagement.pojo.PageBean;
import com.ncepu.clubmanagement.pojo.Result;
import com.ncepu.clubmanagement.service.AffairService;
import com.ncepu.clubmanagement.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/affair")
public class AffairController {

    @Autowired
    private JwtUtils jwtUtil;

    @Autowired
    private AffairService affairService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/generate")
    public Result addRequestAffair(@RequestBody Affair affair) throws Exception {
        log.info("添加请求事务");
        // jwt 解析账户
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer id = (Integer)claims.get("id");

        // 根据jwt将id赋给事务
        affair.setInitiatorId(id);

        affairService.addAffair(null,affair);   // 处理事件
        return Result.success();
    }

    @PostMapping("/generate/multimedia")
    public Result addRequestAffairOfMultiMedia(@RequestParam(value = "cover",required = false)MultipartFile cover,
                                               @RequestParam(value = "multimedia",required = false)MultipartFile multimedia,
                                               Affair affair ) throws Exception {
        log.info("添加请求事务(多媒体类型)");
        // jwt 解析账户
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer id = (Integer)claims.get("id");

        // 根据jwt将id赋给事务
        affair.setInitiatorId(id);

        // 构造事务所需的文件map源
        Map<String, MultipartFile> affairMap =
                new HashMap<>(){{put("cover",cover); put("multimedia",multimedia);}};

        affairService.addAffair(affairMap,affair);
        return Result.success();
    }


    @PostMapping("/generate/haveFile")
    public Result addRequestAffairWithFile(@RequestParam(value = "image",required = false)MultipartFile image, Affair affair ) throws Exception {
        log.info("添加请求事务");
        // jwt 解析账户
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer id = (Integer)claims.get("id");

        // 根据jwt将id赋给事务
        affair.setInitiatorId(id);

        Map<String, MultipartFile> affairMap = new HashMap<>(){{put("image",image);}};

        affairService.addAffair(affairMap,affair);
        return Result.success();
    }

    @GetMapping()
    public Result getAffair(Integer type, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")  Integer pageSize){
        log.info("查询事务");
        // jwt 解析账户
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer permission = (Integer) claims.get("permission");
        Integer personId = (Integer)claims.get("id");


        PageBean pageBean  = affairService.getAffair(type, permission,page,pageSize, personId);

        return Result.success(pageBean);
    }

    // 事务同意逻辑
    @PutMapping("/pass")
    public Result handlePassedAffair(Integer id) throws Exception {
        log.info("处理事务通过逻辑");
        // jwt 解析账户
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer permission = (Integer) claims.get("permission");
        Integer personId = (Integer)claims.get("id");

        if(affairService.handlePassedAffair(id, permission))
            return Result.success();
        return Result.error("事务不存在或事务处理细节错误，联系管理员");
    }

    @PutMapping("/reject")
    public Result handleRejectAffair(Integer id){
        log.info("处理事务拒绝逻辑");

        // jwt 解析账户
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer permission = (Integer) claims.get("permission");

        affairService.handleRejectAffair(id, permission);
        return Result.success();
    }

    @GetMapping("/content")
    public Result getAffairContent(Integer id) throws NotExistException {
        log.info("查询事务详情");

        // jwt 解析账户
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer permission = (Integer) claims.get("permission");

        AffairContent affairContent = affairService.getAffairContent(id,permission);

        return Result.success(affairContent);
    }
}
